function CCompl = geneGSFile(filenaIn,filenaOut,N,dty,UTMzone,NameID)
%geneGSFile - generation of a .txt file for GridStrain
%
%       CCompl = geneGSFile(CellIn,FilenaOut,N,DTY,UTMZzone,NameID)
%
% This function generates the .txt file FilenaOut suitable for GridStrain 
% (2D or 2.5D) on the basis of:
%   - Input cell data CellIn provided by a previous StaVelMain session.
%     Options:
%       - CellIn is a cell variable available in MATLAB command window;
%       - CellIn is a string (including or not includind the extension
%         '.mat') with the filename of the MATLAB file which contains
%         the cell variable. The file must contain no more than a field;
%       - CellIn is undefined or empty. In this case, the filename is
%         interactively managed.
%   - Name of the output ASCII file filenaOut. If filenaOut is undefined 
%     or empty, the filename can be interactively managed.
%   - Number of coordinates to be taken into account. Valid choices are 2
%     (horizontal components only) and 3 (xyz coordinates). If N is invalid,
%     undefined or empty, N = 2 is used.
%   - Time series minimum duration, in years, dty. The stations whose
%     time series are shorter than dty are excluded from the output file.
%     If dty is undefined or empty, no a minimum duration is taken into
%     account.
%   - UTM zone expressed by the string UTMzone (e.g. '33N'). If UTMzone 
%     is undefined or empty, the default UTM zone which corresponds to
%     the WGS84 coordinates is chosen.
%   - NameID. If NameID is undefined, empty or false, the station IDs are 
%     the station numbers. If NameID is true, the station IDs are the
%     station names.
%
% See also StaVelMain.   

% G. Teza, 2022

if (nargin < 6) || isempty(NameID)
    NameID = false;
end

if nargin < 5
    UTMzone = [];
end

if ~isempty(UTMzone)
    if ischar(UTMzone)
        UTMz = str2double(UTMzone(1:end-1));
        UTMh = UTMzone(end);
    else
        UTMz = UTMzone;
        UTMh = 'N';
    end
end
    
if nargin < 4
    dty = [];
end

if nargin < 3 || isempty(N) || ~ismember(N,[2 3])
    N = 2;
end

if nargin < 2 || isempty(filenaOut)
    [filename, pathname] = uiputfile( ...
        {'*.txt', 'ASCII Files (*.txt)'}, ...
        'OUTPUT FILE FOR GRID_STRAIN/GRID_STRAIN3');
    if isequal(filename,0) || isequal(pathname,0)
        disp('Invalid output file')
        CCompl = [];
        return
    else
        filenaOut = fullfile(pathname,filename);
    end
end

if (nargin < 1) || isempty(filenaIn)
    filenaIn = '';
end
if ischar(filenaIn)
    if isempty(filenaIn) 
        [filename, pathname] = uigetfile(...
            {'*.mat','MATLAB Files (*.mat)'},...
            'INPUT MATLAB FILE');
        if isequal(filename,0) || isequal(pathname,0)
            disp('Invalid input data')
            CCompl = [];
            return
        else
            filenaIn = fullfile(pathname,filename);
        end
    end
    CIns = load(filenaIn);
    fCIns = fieldnames(CIns);
    if numel(fCIns) > 1
        disp('Invalid input data')
        CCompl = [];
        return
    end
    CIn = CIns.(char(fCIns));
elseif iscell(filenaIn)
    CIn = filenaIn;
else
    disp('Invalid input data')
    CCompl = [];
    return
end

nsin = size(CIn,1);    % initial number of stations 
n = (1:nsin)';
if N == 2
    ncol = 9;
else
    ncol = 12;
end

CCompl = cell(nsin,ncol);
for k = 1:nsin
    
    tik = CIn{k,2};
    if ~isempty(tik)
        if ~isempty(dty)
            tfk = CIn{k,3};
            if (tfk-tik)/365.25 < dty
                n(k) = NaN;
            end
        end
    else
        n(k) = NaN;
    end
    
    latk = CIn{k,4};
    if isempty(latk)
        n(k) = NaN;
    end
    if isnan(n(k))
        continue
    end
    lonk = CIn{k,5};
    xEtk = CIn(k,7);
    yNtk = CIn(k,8);
    xEtsk = CIn(k,10);
    yNtsk = CIn(k,11);
    
    if ~isempty(UTMzone)
        [xEk,yNk] = wgs2utm(latk,lonk,UTMz,UTMh);
        UTMzonek = UTMzone;
    else
        [xEk,yNk,UTMzOutk,UTMhOutk] = wgs2utm(latk,lonk);
        UTMzonek = [num2str(UTMzOutk) UTMhOutk];
    end
    
    if NameID
        CCompl(k,1) = CIn(k,1);
    else
        CCompl(k,1) = {n(k)};
    end
    CCompl(k,2) = CIn(k,1);
    CCompl(k,3) = {xEk};
    CCompl(k,4) = {yNk};     
    CCompl(k,end) = {UTMzonek};
    
    if N == 2
        CCompl(k,5) = xEtk;
        CCompl(k,6) = yNtk;
        CCompl(k,7) = xEtsk;
        CCompl(k,8) = yNtsk;
    else
        zHk   = CIn(k,6);
        zHtk  = CIn(k,9);
        zHtsk = CIn(k,12);
        CCompl(k,5) = zHk;
        CCompl(k,6) = xEtk;
        CCompl(k,7) = yNtk;
        CCompl(k,8) = zHtk;
        CCompl(k,9) = xEtsk;
        CCompl(k,10) = yNtsk;
        CCompl(k,11) = zHtsk;
    end
    
end
if sum(isnan(n)) > 0
    Ina = isnan(n);
    CCompl(Ina,:) = [];
end

Csave = CCompl;
Csave(:,2) = [];
Csave(:,end) = [];
writecell(Csave,filenaOut,'delimiter','\t');